package gov.jyy.xzxk.xksq.common.desensitization.impl;

import gov.jyy.xzxk.xksq.common.desensitization.Desensitization;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;


/**
 * 日期类型的脱敏器
 *
 */
public class DateNoDesensitization implements Desensitization<Object> {

    private final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    private final static String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";


    @Override
    public Object desensitize(Object data) {
        if(data instanceof String) {
            String date = (String) data;
            return date.replaceAll("\\d", "*");
        }
        String formattedDate = null;
        if(data instanceof Date) {
            Date date = (Date) data;
            SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
            formattedDate = formatter.format(date);
        }
        if(data instanceof LocalDate) {
            LocalDate localDate = (LocalDate) data;
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
            formattedDate = formatter.format(localDate);
        }
        if(data instanceof LocalDateTime) {
            LocalDateTime localDate = (LocalDateTime) data;
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
            formattedDate = formatter.format(localDate);
        }
        if(formattedDate != null) {
            return formattedDate.replaceAll("\\d", "*");
        }
        return data;
    }

}